<?php
class RoomController extends Controller
{
	public $layout="//layouts/nolayout";

	public function actionShowRoomInfoPage()
	{
		$this->render('showRoomInfoPage');
	}
	
	public function actionShowRoomInfoOnlyPage()
	{
		$this->render('showRoomInfoOnlyPage');
	}
	public function actionShowEnvironmentInfoPage()
	{
		$this->render('showEnvironmentInfoPage');
	}
	
	public function actionShowOpenRoomPage()
	{
		$this->render('showOpenRoomPage');
	}
	
	public function actionShowVirtualRoomPage()
	{
		$this->render('showVirtualRoomPage');
	}
	
	//添加新实验室
	public function actionAddNewRoom()
	{
		if(isset($_POST['roomName'])&&isset($_POST['user_id']))
		{
			$newRoom=new Room;
			$newRoom->name=$_POST['roomName'];
			$res=Room::model()->findByAttributes(array('name'=>$_POST['roomName']));
			if (!empty($res))
			{
				$json=array(  
							'success'=>false,
							'res'=>'添加失败！已有同名实验室存在！'
						);   
				echo CJSON::encode($json);
				return;
			}
			$newRoom->admin_user_id=$_POST['user_id'];
			$newRoom->num_of_computer=$_POST['num_of_computer'];
			$newRoom->description=$_POST['description'];
			if ($newRoom->save()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>$newRoom->room_id
						);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
							'success'=>false,
							'res'=>'添加失败！'
						);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'数据发送失败！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionSetEnvironmentRoom($rid,$eid,$status)
	{
		$environment_room=new Environment_Room;
		$environment_room->environment_id=$eid;
		$environment_room->room_id=$rid;
		if ($status=='true')
		{
			$res=Environment_Room::model()->findByAttributes(array('room_id'=>$rid,'environment_id'=>$eid));
			if (empty($res))
				$environment_room->save();
		}else{
			$res=Environment_Room::model()->findByAttributes(array('room_id'=>$rid,'environment_id'=>$eid));
			if (!empty($res))
				$res->delete();
		}
	}
	
	//获取环境与实验室绑定的选定状态
	public function actionGetERChecked($rid)
	{
		$e_rList=Environment_Room::model()->findAllByAttributes(array('room_id'=>$rid));
		$json=array();
		for ($i=0;$i<count($e_rList);$i++)
		{
			$elem=array();
			$elem['eid']=$e_rList[$i]->environment_id;
			array_push($json,$elem);
		}
		echo CJSON::encode($json);
	}
	
	//设置虚拟实验与实验室绑定
	public function actionSetCourseRoom($rid,$cid,$status)
	{
		$course_room=new Course_Room;
		$course_room->course_id=$cid;
		$course_room->room_id=$rid;
		
		$res=Course_Room::model()->findByAttributes(
			array(
				'room_id'=>$rid,
				'course_id'=>$cid,
				)
			);
		if ($status=='true')
		{
			if (empty($res))
				$course_room->save()>0;
		}else{
			if (!empty($res))
				$res->delete();
		}
	}
	
	//获取虚拟实验与实验室绑定的选定状态
	public function actionGetCRChecked($rid)
	{
		$c_rList=Course_Room::model()->findAllByAttributes(
										array(
										'room_id'=>$rid,
										)
									);
		$json=array();
		for ($i=0;$i<count($c_rList);$i++)
		{
			$elem=array();
			$elem['cid']=$c_rList[$i]->course_id;
			array_push($json,$elem);
		}
		echo CJSON::encode($json);
	}
	
	//取得实验室管理员
	public function actionGetRoomAdmin($rid)
	{
		$roomAdmin=Room::model()->findByAttributes(array('room_id'=>$rid));
		$json=array();
		$json['admin_user_id']=$roomAdmin->admin_user_id;
		echo CJSON::encode($json);
	}
	
	//呈现已有环境表格
	public function actionGetEnvironmentGrid()
	{
		$json=Environment::model()->findAll();
		echo CJSON::encode($json);
	}
	
	//获取实验室列表
	public function actionGetRoomList()
	{
		$ARlist=Room::model()->findAll(array('order'=>'name ASC'));
		$json=array();
		$count = 0;
		foreach($ARlist as $elem)
		{
			array_push($json,$elem->attributes);
			$json[$count++]['admin_name'] = '<div id="tips" class="TipDiv" data-qtip="'.$elem->adminUser->name.'<br> tel:'.$elem->adminUser->tel.'" data-qtitle="联系方式" data-qwidth="400">'.$elem->adminUser->name.'</div>';
		}
		echo CJSON::encode($json);
	}
	
	public function actionEditRoom()
	{
		if(isset($_POST['room_id'])&&isset($_POST['user_id']))
		{
			$editRoom=new Room;
			$res=Room::model()->findByAttributes(array('room_id'=>$_POST['room_id']));
			if (empty($res))
			{
				$json=array(  
							'success'=>false,
							'res'=>'修改失败！无该实验室存在！'
						);   
				echo CJSON::encode($json);
				return;
			}
			//若改名
			if (!empty($_POST['roomName']))
			{
				$res->name=$_POST['roomName'];
			}
			//替换管理员
			$res->admin_user_id=$_POST['user_id'];
			$res->description=$_POST['description'];
			$res->num_of_computer=$_POST['num_of_computer'];
			//成功
			if ($res->save()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>$_POST['room_id']
						);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
							'success'=>false,
							'res'=>'修改失败！'
						);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'数据发送失败！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionDeleteRoomPage()
	{
		$this->render('deleteRoomPage');
	}
	
	public function actionDeleteRoom()
	{
		if (isset($_POST['room_id']))
		{
			$delRoom=Room::model()->findByPk($_POST['room_id']);
			if ($delRoom->delete()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>'成功删除！'
						);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
							'success'=>false,
							'res'=>'删除失败！'
						);   
				echo CJSON::encode($json);
			}
		}else{
				$json=array(  
							'success'=>false,
							'res'=>'网络传输失败！'
						);   
				echo CJSON::encode($json);
		}
	}

	//添加新环境
	public function actionAddNewEnvironment()
	{
		if (isset($_POST['name'])&&isset($_POST['information']))
		{
			$newEnvir=new Environment;
			$newEnvir->name=$_POST['name'];
			$newEnvir->information=$_POST['information'];
			if ($newEnvir->save()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>'成功添加！'
						);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
							'success'=>false,
							'res'=>'添加失败！'
						);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'数据发送失败！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionDeleteEnvironmentPage()
	{
		$this->render('deleteEnvironmentPage');
	}
	
	public function actionEditEnvironment()
	{
		if (isset($_POST['environment_id'])&&isset($_POST['name'])&&isset($_POST['information']))
		{
			$newEnvir=Environment::model()->findByPk($_POST['environment_id']);
			$newEnvir->name=$_POST['name'];
			$newEnvir->information=$_POST['information'];
			if ($newEnvir->save()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>'成功修改！'
						);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
							'success'=>false,
							'res'=>'修改失败！'
						);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'数据发送失败！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionDeleteEnvironment()
	{
		if (isset($_POST['environment_id']))
		{
			$envir=new Environment;
			$delEnvir=$envir->findByPk($_POST['environment_id']);
			if ($delEnvir->delete()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>'成功删除！'
						);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
							'success'=>false,
							'res'=>'删除失败！'
						);   
				echo CJSON::encode($json);
			}
		}else{
				$json=array(  
							'success'=>false,
							'res'=>'网络传输失败！'
						);   
				echo CJSON::encode($json);
		}
	}
	
	//获得所有环境列表
	public function actionGetEnvironmentList()
	{
		$json=Environment::model()->findAll();
		echo CJSON::encode($json);
	}
	
	
	public function actionOpenRoom($tid,$wid,$rid,$period)
	{
		//查询假期
		$start_date=Week::model()->findByPk($wid)->start_date;
		$periodList=Period::model()->findAll();
		$day=intval($period/(count($periodList)));
		$time=strtotime('+'.$day.' day '.$start_date);
		$date=date('Y-m-d',$time);
		$hasHoliday=Holiday::model()->find("`start_date` <= '".$date."' AND `end_date` >= '".$date."'");
		if (!empty($hasHoliday))
		{
			$json=array(  
						'success'=>false,
						'res'=>'在此有假期！',
					);   
			echo CJSON::encode($json);
			return;
		}
		
		$newOpen=new Open;
		$newOpen->term_id=$tid;
		$newOpen->room_id=$rid;
		$newOpen->week_id=$wid;
		$periodList=Period::model()->findAll();
		$day=intval($period/(count($periodList)));
		$newOpen->day_of_week=intval($day);
		$pindex=$period%(count($periodList));
		$newOpen->period_id=$periodList[$pindex]->period_id;
		$res=Open::model()->findByAttributes(array(
											'term_id'=>$tid,	
											'week_id'=>$wid,
											'day_of_week'=>intval($day),
											'period_id'=>$periodList[$pindex]->period_id,
											'room_id'=>$rid,
											));
		
		if (!empty($res))
		{
			$json=array(
							'success'=>false,
							'res'=>'已经开放了',
						);   
			echo CJSON::encode($json);
		}else{
			if ($newOpen->save()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>'开放成功',
						);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
							'success'=>false,
							'res'=>'开放失败！'
						);   
				echo CJSON::encode($json);
			}
		}
	}
	
	public function actionCloseRoom($tid,$wid,$rid,$period)
	{
		$periodList=Period::model()->findAll();
		$day=$period/(count($periodList));
		$pindex=$period%(count($periodList));
		$res=Open::model()->findByAttributes(array(
											'term_id'=>$tid,	
											'week_id'=>$wid,
											'day_of_week'=>intval($day),
											'period_id'=>$periodList[$pindex]->period_id,
											'room_id'=>$rid,
											));
		if (!empty($res))
		{
			if ($res->delete()>0)
			{
				$json=array(
							'success'=>true,
							'res'=>'关闭成功',
						);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
						'success'=>false,
						'res'=>'无法关闭！'
					);   
				echo CJSON::encode($json);
			}
		}else{
			$json=array(
						'success'=>false,
						'res'=>'当前未开放！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	//检查是否开放
	public function actionHasOpen($tid,$wid,$rid,$period)
	{
		$dayName=array('一','二','三','四','五','六','日');
		$periodList=Period::model()->findAll();
		$day=intval($period/(count($periodList)));
		$pindex=$period%(count($periodList));
		//查询假期
		$start_date=Week::model()->findByPk($wid)->start_date;
		$periodList=Period::model()->findAll();
		$time=strtotime('+'.$day.' day '.$start_date);
		$date=date('Y-m-d',$time);
		$hasHoliday=Holiday::model()->find("`start_date` <= '".$date."' AND `end_date` >= '".$date."'");
		if (!empty($hasHoliday))
		{
			$json=array(  
						'success'=>false,
						'res'=>'<h1 style="color:red">当前为假期时段！</h1>'
					);   
			echo CJSON::encode($json);
			return;
		}
		$hasSchedule=Schedule::model()->findByAttributes(array(
											'term_id'=>$tid,	
											'week_id'=>$wid,
											'day_of_week'=>intval($day),
											'period_id'=>$periodList[$pindex]->period_id,
											'room_id'=>$rid,
											));
		//当前已有课程安排
		if (!empty($hasSchedule))
		{
			$json=array(  
							'success'=>false,
							'title'=>'无法预约',
							'res'=>'当前已有课程',
						);
			echo CJSON::encode($json);
			return;
		}
		
		$isOpened=Open::model()->findByAttributes(array(
											'term_id'=>$tid,	
											'week_id'=>$wid,
											'day_of_week'=>intval($day),
											'period_id'=>$periodList[$pindex]->period_id,
											'room_id'=>$rid,
											));
		
		//当前是否已经开放了
		$res=Open::model()->findByAttributes(array(
											'term_id'=>$tid,	
											'week_id'=>$wid,
											'day_of_week'=>intval($day),
											'period_id'=>$periodList[$pindex]->period_id,
											'room_id'=>$rid,
											));

		$term=Term::model()->findByPK($tid);
		$week=Week::model()->findByPK($wid);
		$room=Room::model()->findByPK($rid);
		if (empty($res))
		{
			$json=array(  
						'success'=>true,
						'title'=>'未开放',
						'res'=>'<h2 style="color:green">点击【开放】以开放当前实验室</h2><p>时间：'.$term->name.' '.$week->week_describe.'</p>'.'<p>日期：['.$date.'] 周'.$dayName[intval($day)].'</p><p>实验室：'.$room->name.'</p><p>节次：'.$periodList[$pindex]->period_info.'</p>',
					);
		}else{
				$json=array(  
							'success'=>true,
							'title'=>'已开放',
							'res'=>'<h2 style="color:blue">点击【关闭】以关闭当前实验室</h2><p>时间：'.$term->name.' '.$week->week_describe.'</p>'.'<p>日期：['.$date.'] 周'.$dayName[intval($day)].'</p><p>实验室：'.$room->name.'</p><p>节次：'.$periodList[$pindex]->period_info.'</p>',
						);
		}
		echo CJSON::encode($json);
	}
	
	public function actionGetCourseRoomList()
	{
		$text='';
		$roomList=Room::model()->findAll();
		for ($i=0;$i<count($roomList);$i++)
		{
			$courseList=Course_Room::model()->findAllByAttributes(array(
											'room_id'=>$roomList[$i]->room_id,
											));
			if (count($courseList)>0)
			{
				$text=$text.'<p style="color:green">'.$roomList[$i]->name.'</p>';
				for ($j=0;$j<count($courseList);$j++)
				{
					$text=$text.'<p>'.($j+1).'、'.$courseList[$j]->course->name.'</p>';
				}
			}
		}
		
		$json=array(  
						'success'=>true,
						'res'=>$text,
					);
		echo CJSON::encode($json);			
	}
}
?>